package com.sneakxy.repository;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.QueryByExampleExecutor;
import org.springframework.stereotype.Repository;

import com.sneakxy.beans.User;
import com.sneakxy.mybatis.commons.annotations.Total;
import com.sneakxy.mybatis.commons.repository.CriteriaRepository;
import com.sneakxy.mybatis.commons.repository.ExpandRepository;

/**
 * @author 潜行的青衣
 * 
 */
@Repository
public interface UserRepository extends 
	CrudRepository<User, Long>, CriteriaRepository<User, Long>, ExpandRepository<User, Long>,
	PagingAndSortingRepository<User, Long>, QueryByExampleExecutor<User> {

	/**
	 * 分页时不使用@Total注解，会自动把第一个select from中间替换为count(*)
	 * @author 潜行的青衣
	 * @version
	 * @param page
	 * @param id
	 * @return
	 */
	@Select("SELECT * FROM t_user WHERE id = #{id}")
	@Total("SELECT COUNT(*) FROM t_user WHERE id = #{id}")
	@ResultMap("User")
	public Page<User> findOneByName(Pageable page, @org.springframework.data.repository.query.Param("id") Long id);
	
	@Select("SELECT * FROM t_user WHERE id = #{id}")
	@ResultMap("User")
	public Page<User> findOneByName2(Pageable page, @Param("id") Long id);
	
	public Page<User> findOneByIdInOrAge(List<Long> ids, int age, Pageable page);
	
	public Slice<User> findOneByIdIn(List<Long> ids, Pageable page);
	
	public List<User> findOneByAgeGreaterThanEqual(int age);
	
	public List<User> findOneByXml(@Param("age") int age);
	
	/**
	 * 最好不要使用STARTING_WITH、ENDING_WITH、CONTAINING、NOT_CONTAINING，无法防止sql注入
	 * @author 潜行的青衣
	 * @version
	 * @param name
	 * @return
	 */
	public List<User> findByNameContaining(String name, Sort sort);
	
}
